@extends('backend.layouts.app')

@section('styles')
<link rel="stylesheet" href="{{asset('/backend/js/ztree/css/metroStyle/metroStyle.css')}}">
@endsection

@section('content')
<div class="layui-row layui-col-space15">
    <div class="layui-col-md2">
        <div class="layui-card">
            <div class="layui-card-header layui-card-header-auto">功能目录</div>
            <div class="layui-card-body">
                <ul class="ztree" id="moduleTree"></ul>
            </div>
        </div>
    </div>
    <div class="layui-col-md10">
        <div class="layui-card">
            <div class="layui-card-header">
                功能信息 </div>
            <div class="layui-card-body">
                <div class="layui-row" style="margin-bottom:20px">
                    <div class="layui-col-md9">
                        &nbsp;
                    </div>
                    <div class="layui-col-md3">
                        <div class="layui-btn-group" style="float:right">
                            <a href="{{route('admin.auth.module.create')}}" class="layui-btn layui-btn-sm">添加</a>
                        </div>
                    </div>
                </div>
                <table class="layui-hide" id="dataTable" lay-filter="dataTable">
                    <script type="text/html" id="options">
                        <div class="layui-btn-group">
                            <a class="layui-btn layui-btn-sm" lay-event="button">按钮</a>
                            <a class="layui-btn layui-btn-sm" lay-event="show">查看</a>
                            <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                            <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
                        </div>

                    </script>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection

@section('script')
<script type="text/javascript" src="{{asset('/backend/js/ztree/js/jquery.ztree.all.js')}}"></script>
<script type="text/javascript">
    layui.use(['layer', 'table', 'form'], function () {
        let table = layui.table;
        let dataTable = table.render({
            elem: '#dataTable',
            title: '功能信息',
            url: '{{route("admin.auth.module.list")}}',
            where: {
                parent_id: 0
            },
            page: true,
            cols: [
                [{
                    field: 'encode',
                    title: '编号',
                    width: 100,
                    fixed: true
                }, {
                    field: 'full_name',
                    width: 120,
                    title: '名称',
                }, {
                    field: 'target',
                    width: 80,
                    title: '目标'
                }, {
                    title: '选项',
                    width: 300,
                    templet: function (res) {
                        let temp = res.is_menu == 1 ?
                            '<input type="checkbox" lay-skin="primary" title="菜单" disabled checked>' :
                            '<input type="checkbox" lay-skin="primary" title="菜单" disabled>';
                        temp = temp + (res.expand_enable == 1 ?
                            '<input type="checkbox" lay-skin="primary" title="展开" disabled checked>' :
                            '<input type="checkbox" lay-skin="primary" title="展开" disabled>'
                        );
                        temp = temp + (res.is_public == 1 ?
                            '<input type="checkbox" lay-skin="primary" title="公共" disabled checked>' :
                            '<input type="checkbox" lay-skin="primary" title="公共" disabled>'
                        );
                        temp = temp + (res.status == 1 ?
                            '<input type="checkbox" lay-skin="primary" title="有效" disabled checked>' :
                            '<input type="checkbox" lay-skin="primary" title="有效" disabled>'
                        );
                        return temp;
                    }
                }, {
                    field: 'url_address',
                    title: '地址'
                }, {
                    fixed: 'right',
                    align: 'center',
                    toolbar: '#options'
                }]
            ]
        });

        table.on('tool(dataTable)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                case 'del':
                    layer.confirm('确认删除吗？', function (index) {
                        $.post("{{ route('admin.auth.module.destroy') }}", {
                                '_method': 'delete',
                                '_token': '{{csrf_token()}}',
                                ids: data.module_id
                            },
                            function (result) {
                                if (result.code == 0) {
                                    obj.del(); //删除对应行（tr）的DOM结构
                                }
                                layer.close(index);
                                layer.msg(result.msg);
                            });
                    });
                    break;
                case 'edit':
                    location.href = "module/" + data.module_id + "/edit";
                    break;
                case 'button':
                    location.href = "module_button/" + data.module_id;
                    break;
                case 'show':
                    location.href = "module/" + data.module_id + "/show";
                    break;
                default:
                    break;
            }
        });

        let zTreeObj;
        $.ajax({
            url: "{{route('admin.auth.module.tree')}}",
            success: function (data) {
                zTreeObj = $.fn.zTree.init($("#moduleTree"), {
                    view: {
                        showIcon: false,
                        showLine: false
                    },
                    callback: {
                        onClick: function (event, treeId, treeNode) {
                            dataTable.reload({
                                where: {
                                    parent_id: treeNode.id
                                },
                                page: {
                                    curr: 1
                                }
                            });
                        }
                    }
                }, data);
            }
        });
    });

</script>
@endsection
